package com.oscar.practice.shiro.biz.shiro;

import java.util.List;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.oscar.practice.shiro.dao.user.PermissionDao;
import com.oscar.practice.shiro.dao.user.RoleDao;
import com.oscar.practice.shiro.dao.user.UserDao;
import com.oscar.practice.shiro.model.user.PermissionInfo;
import com.oscar.practice.shiro.model.user.UserInfo;

/**
 * 自定义权限认证
 *
 * @author tongyufu
 *
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserDao       userDao;
    @Autowired
    private RoleDao       roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("=====doGetAuthorizationInfo=======");
        UserInfo userInfo = (UserInfo) principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo auth = new SimpleAuthorizationInfo();
        List<PermissionInfo> permissions = permissionDao.queryByUserId(userInfo.getId());

        for (PermissionInfo permission : permissions) {
            auth.addStringPermission(permission.getUrl());
        }
        auth.setRoles(roleDao.queryByUserId(userInfo.getId()));
        return auth;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("=====doGetAuthenticationInfo=======");
        String username = (String) token.getPrincipal();
        //String password = new String(((UsernamePasswordToken) token).getPassword());
        UserInfo userInfo = userDao.queryByUsername(username);
        if (userInfo == null) {
            throw new UnknownAccountException();
        }
        if ("1".equals(userInfo.getLocked())) {
            throw new LockedAccountException();
        }
        userDao.updateLoginCount(userInfo.getId());
        SimpleAuthenticationInfo auth = new SimpleAuthenticationInfo(userInfo,
            userInfo.getPassword(), ByteSource.Util.bytes(userInfo.getSalt()), super.getName());
        return auth;
    }

    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }
}
